package com.matt.controller.web;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.vo.materiels.MaterielItemVO;
import com.matt.service.MaterialsService;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

;

@RestController
@RequestMapping("/web/materials")
public class WebMaterialsController {

    @Autowired
    private MaterialsService materialService;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private MemVerificationCache cache;
    
//    @Autowired
//    private RedPointService redPointService;

    @ApiOperation(value = "物资列表",httpMethod = "POST")
    @RequestMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "cid", value = "物资分类id", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "consumable", value = "是否为消耗性物资", required = false, dataType = "boolean"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页码", required = false, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "分页大小", required = false, dataType = "Integer"),
    })
    public StatusResponse list(@RequestHeader String ticket, String cid, Boolean consumable, PagerQuery pagerQuery) {
        String areaId = cache.getAreaId(ticket);
        return StatusResponse.success(materialService.list(areaId, cid, consumable, pagerQuery));
    }

    @ApiOperation(value = "物资分类列表",httpMethod = "POST")
    @RequestMapping("/category")
    public StatusResponse category(@RequestHeader String ticket) {
        String areaId = cache.getAreaId(ticket);
        return StatusResponse.success(materialService.category(areaId));
    }
    @ApiOperation(value = "园区增加物资分类",httpMethod = "POST")
    @RequestMapping("/add_category")
    public StatusResponse addCategory(@RequestHeader String ticket, String name, int sort) {
        String areaId = cache.getAreaId(ticket);
        return StatusResponse.success(materialService.addCategory(areaId, name, sort ));
    }

    @ApiOperation(value = "入库物资",httpMethod = "POST")
    @RequestMapping("/add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "物资id, 提交id的时候为修改相关对象", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "cid", value = "物资分类id", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "物资名字", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "count", value = "数量", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "consumable", value = "是否为消耗性物资", defaultValue = "false", required = false, dataType = "boolean"),
    })
    public StatusResponse add(@RequestHeader String ticket, String id, String cid, String name, Integer count, boolean consumable) {
        String areaId = cache.getAreaId(ticket);
        return StatusResponse.success(materialService.add(areaId, id, cid, name, count, consumable ));
    }

    @ApiOperation(value = "删除物资",httpMethod = "POST")
    @RequestMapping("/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "物资id", required = false, dataType = "string")
    })
    public StatusResponse delete(@RequestHeader String ticket, String id) {
        String areaId = cache.getAreaId(ticket);
        return StatusResponse.success(materialService.delete(areaId, id));
    }

    ///record?type=1&pageNum=0&pageSize=15&name=&startDate=&endDate=
    //type = 1, 借出记录    type=2归还记录
    @ApiOperation(value = "记录查询",httpMethod = "POST")
    @RequestMapping("/record")
    public StatusResponse record(@RequestHeader String ticket, Integer type, String name, String operatorName, String startDate, String endDate, PagerQuery pagerQuery) {
    	StaffDTO baseInfo = cache.getStaff(ticket);
    	String areaId = cache.getAreaId(ticket);
        return StatusResponse.success(materialService.records(baseInfo.getId(),areaId, type, name, operatorName, startDate, endDate,pagerQuery));
    }

    @ApiOperation(value = "增加物资借还领取记录",httpMethod = "POST")
    @RequestMapping("/addRecord")
    //type = 1, 借出记录    type=2归还记录  itemsStr ：mid,count|mid,count
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "1借出记录   2归还记录   3领用记录", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "uid", value = "物资分类id", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "date", value = "操作日期", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "remark", value = "备注", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "itemsStr", value = "物品列表", required = true, dataType = "string"),
    })
    public StatusResponse addRecord(@RequestHeader String ticket, int type, String uid,
                                    String date, String remark, String itemsStr) {

        StaffDTO borrowUser = ssoCallService.getStaffDTO(ticket, uid);
        if(borrowUser == null){
            throw  new BusinessException(ErrorCode.ERROR, "用户不存在");
        }

        String uname = borrowUser.getRealName();

        StaffDTO operator = cache.getStaff(ticket);
        String areaId = operator.getAreaId();
        String operatorName = operator.getRealName();
        String operatorId = operator.getId();

        List<MaterielItemVO> items = new ArrayList<>();
        String[] components = itemsStr.split("\\|");
        if(components.length == 0){
            throw  new BusinessException(ErrorCode.PARAM_CAN_NOT_BLANK, "参数错误");
        }
       
        for(String itemStr : components){
            String[] itemComps = itemStr.split(",");
            if(itemComps.length != 2){
                throw  new BusinessException(ErrorCode.PARAM_CAN_NOT_BLANK, "参数错误1");
            }
            Integer count = Integer.parseInt(itemComps[1]);
            items.add(MaterielItemVO.of(itemComps[0], count));
        }
        return StatusResponse.success(materialService.addRecord(areaId, operatorName, operatorId, uid, uname, type, DateUtil.parseToDay(date), remark, items ));
    }

    @ApiOperation(value = "物资借用列表",httpMethod = "POST")
    @RequestMapping("/borrow")
    public StatusResponse borrow(@RequestHeader String ticket, String uid) {
        String areaId = cache.getAreaId(ticket);
        return StatusResponse.success(materialService.borrow(areaId, uid));

    }
}
